import QtQuick 2.0
import "SlideshowLogic.js" as SwLogic
import "VideoLogic.js" as VideoLogic

Rectangle {
    id: root
    color: "black"
    width: 800
    height: 600
    property string messages_folder: ""

    Image {
        id: llogo
        source: "images/llogo.png"
        anchors.centerIn: parent
        opacity: slide.started || vplayer.started || msg.started ? 0.0 : 0.5
        NumberAnimation on opacity {
            easing.type: Easing.Linear
            duration: 500
        }
    }

    SlideshowImage {
        id: slide
        anchors.fill: parent
        onSlideShowed: SwLogic.onSlideShowed()
    }

    VideoPlayer {
        id: vplayer
        anchors.fill: parent
        onStopped: VideoLogic.onStopped()
    }

    Message {
        id: msg
        anchors.fill: parent
        color: "red"
    }

    RunningLine {
        id: rline
        anchors.bottom: root.bottom
        anchors.left: root.left
        width: root.width
        height: 90
    }

//    Component.onCompleted: {
//        vplayer.source = "http://192.168.7.8:8080/v.html"
//        vplayer.play()
//        vplayer.turnon()
//    }

    Connections {
        target: qrs
        onStartSlideshow: SwLogic.startSlideshow()
        onStopSlideshow: SwLogic.stopSlideshow()
        onSetSlideDelay: slide.duration = delay * 1000

        onStartVideos: VideoLogic.startPlayer()
        onStopVideos: VideoLogic.stopPlayer()

        onStartRunningLine: rline.start()
        onStopRunningLine: rline.stop()
        onSetRunningText: rline.text = text
        onSetRunningSpeed: rline.speed = speed
        onSetRunningColor: rline.color = color

        onStartMessage: msg.start()
        onStopMessage: msg.stop()
        onSetMessageType: msg.image = messages_folder + type
        onSetMessageText: msg.text = text
        onSetMessageColor: msg.color = color

        onSendState: sendState();
    }

    Connections {
        target: ctrl
        onAddSlide: SwLogic.addSlide(path)
        onRemoveSlide: SwLogic.removeSlide(path)
        onAddVideo: VideoLogic.addVideo(path)
        onRemoveVideo: VideoLogic.removeVideo(path)
    }

    function sendState() {
        if (slide.started)
            qrs.slideshowStarted()
        else
            qrs.slideshowStopped()
        qrs.slideDelayChanged(slide.duration / 1000)

        if (rline.started)
            qrs.runningLineStarted()
        else
            qrs.runningLineStopped()
        qrs.runningLineTextChanged(rline.text)

        if (msg.started)
            qrs.messageStarted()
        else
            qrs.messageStopped()
        qrs.messageTextChanged(msg.text)
        qrs.messageColorChanged(msg.color)
    }


    Clock {
        anchors.right: root.right
        anchors.top: root.top
        anchors.margins: 30
    }
}
